package org.misalen.web.admin.controllers;

import org.misalen.utils.TextUtil;
import org.misalen.web.database.model.mysql.pro.ProWebAdvertising;
import org.misalen.web.database.model.mysql.pro.ProWebAdvertisingPreview;
import org.misalen.web.database.model.mysql.pro.ProWebReward;
import org.misalen.web.database.repository.mysql.ProWebAdvertisingPreviewRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(value = "/pro-web-advertising-preview")
public class ProWebAdvertisingPreviewController extends BaseController {

	@Autowired
	protected ProWebAdvertisingPreviewRepository proWebAdvertisingPreviewRepository;

	/**
	 * 列表页
	 * 
	 * @return 列表目标页
	 */
	@RequestMapping(value = "/{primaryKey}", method = { RequestMethod.GET })
	public ModelAndView list(@PathVariable String primaryKey) {
		ModelAndView modelAndView = new ModelAndView("/ProWebAdvertisingPreview/list");
		modelAndView.addObject("info",
				proWebAdvertisingPreviewRepository.get(ProWebAdvertisingPreview.class, primaryKey));
		return modelAndView;
	}

	/**
	 * 列表页
	 * 
	 * @return 列表目标页
	 */
	@RequestMapping(value = "/details/{webId}", method = { RequestMethod.GET })
	public ModelAndView weblist(@PathVariable String webId) {
		ModelAndView modelAndView = new ModelAndView("/ProWebAdvertisingPreview/list");
		modelAndView.addObject("info", proWebAdvertisingRepository.get(ProWebAdvertising.class, webId));
		return modelAndView;
	}

	/**
	 * 列表页
	 * 
	 * @return 列表目标页
	 */
	@RequestMapping(value = "/share/{rewardId}", method = { RequestMethod.GET })
	public ModelAndView sharelist(@PathVariable String rewardId) {
		ModelAndView modelAndView = new ModelAndView("/ProWebAdvertisingPreview/list");
		ProWebReward proWebReward = proWebRewardRepository.get(ProWebReward.class, rewardId);
		if (!TextUtil.stringToBoolean(proWebReward.getTimeLimit())&& proWebReward.getGetTime().getTime() < System.currentTimeMillis() - 24 * 60 * 60 * 1000) {
			proWebReward.setTimeLimit(TextUtil.booleanToString(true));
			proWebRewardRepository.update(proWebReward);
			proUserRepository.updateFreezeBalance(-proWebReward.getMoney(), proWebReward.getUserId());
			proUserRepository.updateBalance(proWebReward.getMoney(), proWebReward.getUserId());
		}
		modelAndView.addObject("info",
				proWebAdvertisingRepository.get(ProWebAdvertising.class, proWebReward.getWebId()));
		return modelAndView;
	}
}
